﻿namespace EFS.RC.CC.Config
{
	public static class Settings
	{
		// sync
		private static object syncAccess = new object();
		
		// loaded flag
		private static System.Boolean loaded = false;

		// Make path
		private static System.String FilePath
		{
			get
			{
				// make path
				return EFS.Common.Directory.MapApplicationPath("EFS.RC.CC.xml");
			}
		}

		#region LoadConfiguration

		/// Load
		private static void Load()
		{
			// load injections
			System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
			try
			{
				// load document
				xmlDocument.Load(FilePath);
			}
			catch (System.Exception e)
			{
				EFS.EventLog.Log.LogException(e, null);
				return;
			}

			// check
			EFS.Common.Verify.Reference(xmlDocument, "xmlDocument");

			// get socrates & check
			System.Xml.XmlElement xmlElement = (System.Xml.XmlElement)xmlDocument.SelectSingleNode("//Settings/CCC");
			EFS.Common.Verify.Reference(xmlElement, "CCC xml element");

			// set
			trustedApplicationUrn = xmlElement.GetAttribute("trustedApplicationUrn");
			centralApplicationId = new System.Guid(xmlElement.GetAttribute("centralApplicationId"));
			agentStatusTrackingRefreshPollingSeconds = System.Int32.Parse(xmlElement.GetAttribute("agentStatusTrackingRefreshPollingSeconds"));
			agentHuntTrackingRefreshPollingSeconds = System.Int32.Parse(xmlElement.GetAttribute("agentHuntTrackingRefreshPollingSeconds"));
			deploymentRefreshPollingMinutes = System.Int32.Parse(xmlElement.GetAttribute("deploymentRefreshPollingMinutes"));
			recorderPath = xmlElement.GetAttribute("recorderPath");
			musicForCalling = xmlElement.GetAttribute("musicForCalling");
			musicForHunting = xmlElement.GetAttribute("musicForHunting");
			musicForHold = xmlElement.GetAttribute("musicForHold");
			musicForOfficeBusy = xmlElement.GetAttribute("musicForOfficeBusy");
			musicForCustomerBusy = xmlElement.GetAttribute("musicForCustomerBusy");
			musicForCustomerDialError = xmlElement.GetAttribute("musicForCustomerDialError");
			musicForAfterHours = xmlElement.GetAttribute("musicForAfterHours");
			dialOutRmove = xmlElement.GetAttribute("dialOutRmove").Split(new char[] { ';' });
			dialOutAdd = xmlElement.GetAttribute("dialOutAdd").Split(new char[] { ';' });
			directDial = xmlElement.GetAttribute("directDial");
			directDialIdentifyUseByExtension = System.Boolean.Parse(xmlElement.GetAttribute("directDialIdentifyUseByExtension"));
			resetCountersAt = System.Int32.Parse(xmlElement.GetAttribute("resetCountersAt"));
			busyAfterSeconds = System.Int32.Parse(xmlElement.GetAttribute("busyAfterSeconds"));
			resumeRecorderAfterSeconds = System.Int32.Parse(xmlElement.GetAttribute("resumeRecorderAfterSeconds"));
			wrapConferenceAfterSeconds = System.Int32.Parse(xmlElement.GetAttribute("wrapConferenceAfterSeconds"));
			agentDialTimeOutAfterSeconds = System.Int32.Parse(xmlElement.GetAttribute("agentDialTimeOutAfterSeconds"));
			directDialPhoneContext = xmlElement.GetAttribute("directDialPhoneContext");
			anonymousPhone = xmlElement.GetAttribute("anonymousPhone");

			// check
			EFS.Common.Verify.Condition(dialOutRmove.Length == dialOutAdd.Length, "dialOutRmove.Length == dialOutAdd.Length");

			// loaded
			loaded = true;
		}
		#endregion

		/// <summary>
		/// Reload config
		/// </summary>
		public static void Reload()
		{
			lock (syncAccess)
			{
				Load();
			}
		}

		/// <summary>
		/// Get desciption
		/// </summary>
		/// <returns></returns>
		public static System.String Describe()
		{
			// ake sb
			System.Text.StringBuilder sb = new System.Text.StringBuilder();

			// add all
			sb.AppendLine(System.String.Format("trustedApplicationUrn {0}", trustedApplicationUrn));
			sb.AppendLine(System.String.Format("centralApplicationId {0}", centralApplicationId));
			sb.AppendLine(System.String.Format("agentStatusTrackingRefreshPollingSeconds {0}", agentStatusTrackingRefreshPollingSeconds));
			sb.AppendLine(System.String.Format("agentHuntTrackingRefreshPollingSeconds {0}", agentHuntTrackingRefreshPollingSeconds));
			sb.AppendLine(System.String.Format("deploymentRefreshPollingMinutes {0}", deploymentRefreshPollingMinutes)); ;
			sb.AppendLine(System.String.Format("recorderPath {0}", recorderPath));
			sb.AppendLine(System.String.Format("musicForCalling {0}", musicForCalling));
			sb.AppendLine(System.String.Format("musicForHunting {0}", musicForHunting));
			sb.AppendLine(System.String.Format("musicForHold {0}", musicForHold));
			sb.AppendLine(System.String.Format("musicForOfficeBusy {0}", musicForOfficeBusy));
			sb.AppendLine(System.String.Format("musicForAfterHours {0}", musicForAfterHours));
			sb.AppendLine(System.String.Format("dialOutRmove {0}", dialOutRmove));
			sb.AppendLine(System.String.Format("dialOutAdd {0}", dialOutAdd));
			sb.AppendLine(System.String.Format("directDial {0}", directDial));
			sb.AppendLine(System.String.Format("directDialIdentifyUseByExtension {0}", directDialIdentifyUseByExtension));
			sb.AppendLine(System.String.Format("resetCountersAt {0}", resetCountersAt));
			sb.AppendLine(System.String.Format("busyAfterSeconds {0}", busyAfterSeconds));
			sb.AppendLine(System.String.Format("wrapConferenceAfterSeconds {0}", wrapConferenceAfterSeconds));
			sb.AppendLine(System.String.Format("directDialPhoneContext {0}", directDialPhoneContext));
			sb.AppendLine(System.String.Format("anonymousPhone {0}", anonymousPhone));

			// done
			return sb.ToString();
		}

		#region Properties

		/// <summary>
		/// AnonymousPhone
		/// </summary>
		private static System.String anonymousPhone = "";
		public static System.String AnonymousPhone
		{
			get
			{
				if (!loaded) { Load(); }
				return anonymousPhone;
			}
		}

		/// <summary>
		/// DirectDialPhoneContext
		/// </summary>
		private static System.String directDialPhoneContext = "";
		public static System.String DirectDialPhoneContext
		{
			get
			{
				if (!loaded) { Load(); }
				return directDialPhoneContext;
			}
		}

		/// <summary>
		/// WrapConferenceAfterSeconds
		/// </summary>
		private static System.Int32 wrapConferenceAfterSeconds = 30;
		public static System.Int32 WrapConferenceAfterSeconds
		{
			get
			{
				if (!loaded) { Load(); }
				return wrapConferenceAfterSeconds;
			}
		}

		/// <summary>
		/// AgentDialTimeOutAfterSeconds
		/// </summary>
		private static System.Int32 agentDialTimeOutAfterSeconds = 10;
		public static System.Int32 AgentDialTimeOutAfterSeconds
		{
			get
			{
				if (!loaded) { Load(); }
				return agentDialTimeOutAfterSeconds;
			}
		}

		/// <summary>
		/// BusyAfterSeconds
		/// </summary>
		private static System.Int32 busyAfterSeconds = 3;
		public static System.Int32 BusyAfterSeconds
		{
			get
			{
				if (!loaded) { Load(); }
				return busyAfterSeconds;
			}
		}

		/// <summary>
		/// ResumeRecorderAfterSeconds
		/// </summary>
		private static System.Int32 resumeRecorderAfterSeconds = 3;
		public static System.Int32 ResumeRecorderAfterSeconds
		{
			get
			{
				if (!loaded) { Load(); }
				return resumeRecorderAfterSeconds;
			}
		}

		/// <summary>
		/// ResetCountersAt
		/// </summary>
		private static System.Int32 resetCountersAt = 3;
		public static System.Int32 ResetCountersAt
		{
			get
			{
				if (!loaded) { Load(); }
				return resetCountersAt;
			}
		}

		/// <summary>
		/// CentralApplicationId
		/// </summary>
		private static System.Guid centralApplicationId = System.Guid.Empty;
		public static System.Guid CentralApplicationId
		{
			get
			{
				if (!loaded) { Load(); }
				return centralApplicationId;
			}
		}

		/// <summary>
		/// TrustedApplicationUrn
		/// </summary>
		private static System.String trustedApplicationUrn = "urn:application:contactcenter";
		public static System.String TrustedApplicationUrn
		{
			get
			{
				if (!loaded) { Load(); }
				return trustedApplicationUrn;
			}
		}

		/// <summary>
		/// DeploymentRefreshPollingMinutes
		/// </summary>
		private static System.Int32 deploymentRefreshPollingMinutes = 10;
		public static System.Int32 DeploymentRefreshPollingMinutes
		{
			get
			{
				if (!loaded) { Load(); }
				return deploymentRefreshPollingMinutes;
			}
		}

		/// <summary>
		/// AgentStatusTrackingRefreshPollingSeconds
		/// </summary>
		private static System.Int32 agentStatusTrackingRefreshPollingSeconds = 3;
		public static System.Int32 AgentStatusTrackingRefreshPollingSeconds
		{
			get
			{
				if (!loaded) { Load(); }
				return agentStatusTrackingRefreshPollingSeconds;
			}
		}

		/// <summary>
		/// AgentHuntTrackingRefreshPollingSeconds
		/// </summary>
		private static System.Int32 agentHuntTrackingRefreshPollingSeconds = 3;
		public static System.Int32 AgentHuntTrackingRefreshPollingSeconds
		{
			get
			{
				if (!loaded) { Load(); }
				return agentHuntTrackingRefreshPollingSeconds;
			}
		}

		/// <summary>
		/// RecorderPath
		/// </summary>
		private static System.String recorderPath = null;
		public static System.String RecorderPath
		{
			get
			{
				if (!loaded) { Load(); }
				return recorderPath;
			}
		}

		/// <summary>
		/// MusicForCustomerBusy
		/// </summary>
		private static System.String musicForCustomerBusy = null;
		public static System.String MusicForCustomerBusy
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForCustomerBusy;
			}
		}

		/// <summary>
		/// MusicForCustomerDialError
		/// </summary> 
		private static System.String musicForCustomerDialError = null;
		public static System.String MusicForCustomerDialError
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForCustomerDialError;
			}
		}

		/// <summary>
		/// MusicForOfficeBusy
		/// </summary>
		private static System.String musicForOfficeBusy = null;
		public static System.String MusicForOfficeBusy
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForOfficeBusy;
			}
		}

		/// <summary>
		/// MusicForHold
		/// </summary>
		private static System.String musicForHold = null;
		public static System.String MusicForHold
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForHold;
			}
		}

		/// <summary>
		/// MusicForAfterHours
		/// </summary>
		private static System.String musicForAfterHours = null;
		public static System.String MusicForAfterHours
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForAfterHours;
			}
		}

		/// <summary>
		/// MusicForCalling
		/// </summary>
		private static System.String musicForCalling = null;
		public static System.String MusicForCalling
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForCalling;
			}
		}

		/// <summary>
		/// MusicForHunting
		/// </summary>
		private static System.String musicForHunting = null;
		public static System.String MusicForHunting
		{
			get
			{
				if (!loaded) { Load(); }
				return musicForHunting;
			}
		}

		/// <summary>
		/// DialOutRmove
		/// </summary>
		private static System.String[] dialOutRmove = null;
		public static System.String[] DialOutRmove
		{
			get
			{
				if (!loaded) { Load(); }
				return dialOutRmove;
			}
		}

		/// <summary>
		/// DialOutAdd
		/// </summary>
		private static System.String[] dialOutAdd = null;
		public static System.String[] DialOutAdd
		{
			get
			{
				if (!loaded) { Load(); }
				return dialOutAdd;
			}
		}

		/// <summary>
		/// DirectDial
		/// </summary>
		private static System.String directDial = null;
		public static System.String DirectDial
		{
			get
			{
				if (!loaded) { Load(); }
				return directDial;
			}
		}

		/// <summary>
		/// DirectDial
		/// </summary>
		private static System.Boolean directDialIdentifyUseByExtension = false;
		public static System.Boolean DirectDialIdentifyUseByExtension
		{
			get
			{
				if (!loaded) { Load(); }
				return directDialIdentifyUseByExtension;
			}
		}

		#endregion
	}

}